﻿// 
//  Copyright 2011  comphilip
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
using System;
using System.Globalization;
using System.IO;
using System.Linq;

namespace Dict4Engkoo.Engkoo {
	internal static class EngkooResultFormator {
		public static void FormatResult(TextWriter Writer, EngkooQueryResult EngkooResult){
			var formators = new Action<TextWriter, EngkooQueryResult>[]{
				FormatPronunciation,
				FormatDefine,
				FormatPhrase,
				FormatSentence,
				FormatInfections,
				FormatCollocations,
				FormatThesauruses
			};
			foreach(var item in formators){
				item(Writer, EngkooResult);
			}
		}
		
		private static void FormatPronunciation(TextWriter Writer, EngkooQueryResult EngkooResult){
			if (!EngkooResult.Pronunciations.Any())
				return;
			
			Writer.WriteLine(Strings.EngkooPron);
			foreach(var item in EngkooResult.Pronunciations){
				Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t{0} [{1}]", item.Lang, item.PhoneticSymbol));	
			}
			Writer.WriteLine();
		}
		
		private static void FormatDefine(TextWriter Writer, EngkooQueryResult EngkooResult){
			if (!EngkooResult.Defines.Any())
				return;
			
			Writer.WriteLine(Strings.EngkooDefine);
			int count = 1;
			foreach(var item in EngkooResult.Defines){
				Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t{0}. {1}", count++, item.Character));
				foreach(var exp in item.Explanations){
					foreach(var printItem in new string[]{exp.Explanation1, exp.Explanation2, exp.Sentence1, exp.Sentence2}){
						if (!string.IsNullOrEmpty(printItem))
							Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t\t{0}", printItem));
					}
					Writer.WriteLine();
				}
			}
			
			Writer.WriteLine();
		}
		
		private static void FormatPhrase(TextWriter Writer, EngkooQueryResult EngkooResult){
			if (!EngkooResult.Phrases.Any())
				return;
			
			Writer.WriteLine(Strings.EngkooPhrase);
			foreach(var item in EngkooResult.Phrases){
				Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t{0}", item.Key));
				Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t{0}", item.Value));
				Writer.WriteLine();
			}
		}
		
		private static void FormatSentence(TextWriter Writer, EngkooQueryResult EngkooResult){
			if (!EngkooResult.Sentences.Any())
				return;
			
			Writer.WriteLine(Strings.EngkooSentence);
			foreach(var item in EngkooResult.Sentences){
				Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t{0}", item.Key));
				Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t{0}", item.Value));
				Writer.WriteLine();
			}
		}
		
		private static void FormatInfections(TextWriter Writer, EngkooQueryResult EngkooResult){
			if (!EngkooResult.Infections.Any())
				return;
			
			Writer.WriteLine(Strings.EngkooInfection);
			foreach(var item in EngkooResult.Infections){
				Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t{0}", item.Type));
				Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t{0}", item.Word));
				Writer.WriteLine();
			}
		}
		
		private static void FormatCollocations(TextWriter Writer, EngkooQueryResult EngkooResult){
			if (!EngkooResult.Collocations.Any())
				return;
			
			Writer.WriteLine(Strings.EngkooCollocation);
			foreach(var item in EngkooResult.Collocations){
				Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t{0}", item.Relation));
				foreach(var coll in item.Collocations)
					Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t\t{0}", coll));
			}
			Writer.WriteLine();
		}
		
		private static void FormatThesauruses(TextWriter Writer, EngkooQueryResult EngkooResult){
			if (!EngkooResult.Thesauruses.Any())
				return;
			
			Writer.WriteLine(Strings.EngkooThesauruse);
			foreach(var item in EngkooResult.Thesauruses){
				Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t{0}", item.Character));
				if (item.Synonyms.Any()){
					Writer.WriteLine("\t" + Strings.EngkooSynonyms);
					foreach(var synonym in item.Synonyms)
						Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t\t{0}", synonym));
				}
				if (item.Antonyms.Any()){
					Writer.WriteLine("\t" + Strings.EngkooAntonyms);
					foreach(var antonym in item.Antonyms)
						Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "\t\t{0}", antonym));
				}
			}
			Writer.WriteLine();
		}
	}
}

